package com.inspur.demo.web;

import com.inspur.demo.core.result.Result;
import com.inspur.demo.model.User;
import com.inspur.demo.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @ClassName UserController
 * @Description TODO
 * @Company inspur
 * @Author Mark.Zhang
 * @Date 2019/5/16 10:18
 * @Version 1.0 
 */
@RestController
@Api(value = "用户管理类", description = "管理用户的增删改查")
@CacheConfig(cacheNames = "userCache")
public class UserController {
    @Autowired
    IUserService userService;

//    http://localhost:8088/demo/createUser?name=jim&&age=3
    @RequestMapping(value = "/createUser")
    @ApiOperation(value = "用户新增", notes = "根据用户名称和年龄创建用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "用户名", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "age", value = "年龄", paramType = "query", dataType = "int", required = true)
    })
    public User create(String name, int age) {
        User user = new User(name, age, new Date());
        User u = userService.create(user);// 进service层
        return u;
    }

    @RequestMapping(value = "/findUser/{id}")
    @ApiOperation(value = "用户查询", notes = "根据ID查询用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", paramType = "path", dataType = "String", required = true)
    })
    @Cacheable(key = "#id")
    public Result create(@PathVariable String id) {
        User u = userService.find(id);
        return Result.success(u);
    }

}
